package com.weavernorth.humanaffairs;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;


import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时任务调动更新店铺信息表店经理字段
 */


public class TransferUpStoreTimer extends BaseCronJob {
    public static Log log = LogFactory.getLog(TransferUpStoreTimer.class.getName());

    @Override
    public void execute() {
        BaseBean bb = new BaseBean();
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsUp = new RecordSet();
        //当前日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String strCurr = simpleDateFormat.format(new Date());

        //获取调动表名
        String strTransferTable = Util.null2String(bb.getPropValue("mangerjobname", "transfertable"));

        if (!"".equals(strTransferTable)) {
            //多个表以逗号分隔查询
            String[] arrTable = strTransferTable.split(",");
            for (int i = 0; i < arrTable.length; i++) {
                //查询当前数据
                String strSql = "select  *  from " + arrTable[i] + " where  rzsj=?";
                rs.executeQuery(strSql, strCurr);
                while (rs.next()) {
                    //调动前岗位id
                    String strBeforeJobId = Util.null2String(rs.getString("gwmc"));
                    //调动前岗位名称
                    String strBeforeJobname = getJobName(strBeforeJobId);
                    //调动后岗位id
                    String strBehindJobId = Util.null2String(rs.getString("gwmch"));
                    //调动后岗位名称
                    String strBehindJobName = getJobName(strBehindJobId);
                    //调动前部门
                    String strBeforeBm = Util.null2String(rs.getString("bmh"));
                    //调动后部门
                    String strBehindBm = Util.null2String(rs.getString("bm"));
                    //调动人id
                    String strUserid = Util.null2String(rs.getString("ddrxm"));
                    //获取店经理的名称
                    String strMangername="";
                    try{
                        strMangername = new String(bb.getPropValue("mangerjobname", "mangername").getBytes("iso8859-1"), "utf-8");
                        log.info("配置文件中,岗位名称:"+strMangername);
                    }catch (UnsupportedEncodingException e){
                        e.printStackTrace();
                    }
                    //如果调动前是店经理
                    if (strBeforeJobname.equals(strMangername)) {
                        String strUpsql = "update uf_dpxx set dc='' where dp=?";
                        log.info("调动前:调动流程更新店铺信息:" + strUpsql + ",部门:" + strBeforeBm);
                        rsUp.executeUpdate(strUpsql, strBeforeBm);
                    }
                    //如果调动后是店经理
                    if (strBehindJobName.equals(strMangername)) {
                        String strUpsql = "update uf_dpxx set dc='" + strUserid + "' where dp=?";
                        log.info("调动后:调动流程更新店铺信息:" + strUpsql + ",部门:" + strBehindBm);
                        rsUp.executeUpdate(strUpsql, strBehindBm);
                    }

                }


            }


        }


    }

    /**
     * 根据岗位id查询岗位名称
     */
    public String getJobName(String strJobid) {
        //岗位名称
        String strJobName = "";
        //数据库操作
        RecordSet rs = new RecordSet();
        String sql = "select jobtitlename from hrmjobtitles where id=?";
        rs.executeQuery(sql, strJobid);
        if (rs.next()) {
            strJobName = Util.null2String(rs.getString("jobtitlename"));
        }
        return strJobName;

    }

}



